<!-- 品牌信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                <a-form-item label="安卓版本号">
                    <a-input v-model="config.androidVersionCode" disabled placeholder="请输入安卓版本号" allow-clear></a-input>
                    <a-upload-dragger
                        name="file"
                        @change="handleChangeAndroid"
                        :before-upload="beforeUpload"
                        :fileList = androidFileList
                        :remove="()=>{androidFileList = []}"
                    >
                        <p class="ant-upload-drag-icon">
                        <a-icon type="inbox" />
                        </p>
                        <p class="ant-upload-text">
                            上传APK文件包（安卓安装包）获取安卓版本号
                        </p>
                    </a-upload-dragger>
                    <a-button type="primary" :loading="confirmLoading" v-if="showAPKUpload" @click="updateAPK">确定上传</a-button>
                </a-form-item>
                <a-form-item label="安卓版本名称">
                    <a-input v-model="config.androidVersionName" disabled placeholder="请输入安卓下载地址" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="安卓包名称">
                    <a-input v-model="config.androidPackageName" disabled placeholder="请输入安卓下载地址" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="安卓是否强制更新">
                    <a-radio-group :options="options" v-model="config.androidIsForceUpdate" />
                </a-form-item>
                <a-form-item label="安卓下载地址">
                    <a-input v-model="config.androidDownloadUrl" placeholder="请输入安卓下载地址" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="安卓三方下载链接">
                    <a-input v-model="config.externalDownloadUrl" placeholder="请输入web下载链接" allow-clear></a-input>
                </a-form-item>
                

                <a-form-item label="安卓升级说明">
                    <a-input v-model="config.androidContent" type="textarea" placeholder="请输入安卓升级说明" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="苹果版本号">
                    <a-input v-model="config.iosVersion" placeholder="请输入苹果版本号" allow-clear></a-input>
                    <a-upload-dragger
                        name="file"
                        @change="handleChangeIos"
                        :before-upload="beforeUpload"
                        :fileList = iosFileList
                        :remove="()=>{iosFileList = []}"
                    >
                        <p class="ant-upload-drag-icon">
                        <a-icon type="inbox" />
                        </p>
                        <p class="ant-upload-text">
                            上传IPA文件包（苹果安装包）获取苹果版本号
                        </p>
                    </a-upload-dragger>
                    <a-button type="primary" :loading="confirmLoading" v-if="showIPAUpload" @click="updateIPA">确定上传</a-button>
                </a-form-item>
                <a-form-item label="苹果下载地址">
                    <a-input v-model="config.iosDownloadUrl" placeholder="请输入苹果下载地址" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="苹果升级说明">
                    <a-input v-model="config.iosContent" type="textarea" placeholder="请输入苹果升级说明" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="分享地址">
                    <a-input v-model="config.shareUrl" placeholder="请输入分享地址" allow-clear></a-input>
                </a-form-item>
                <a-form-item label="苹果三方下载链接">
                    <a-input v-model="config.iosExternalDownloadUrl" placeholder="请输入web下载链接" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="苹果版本名称">
                    <a-input v-model="config.iosVersionName" placeholder="请输入苹果版本名称" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="客服电话">
                    <a-input v-model="config.phone" placeholder="请输入客服电话" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="客服微信号码">
                    <a-input v-model="config.wechat" placeholder="请输入客服微信号码" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="实名次数">
                    <a-input v-model="config.maxRealNameAuthCount" placeholder="请输入单用户最大实名次数" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="奖励扣除比例" help="0.1表示扣除 10%">
                    <a-input v-model="config.deductRate" placeholder="请输入奖励扣除比例" allow-clear></a-input>
                </a-form-item>
                 <a-form-item label="引流任务奖励总金额" >
                    <a-input v-model="config.registerTaskAmount" placeholder="请输入引流任务奖励总金额" allow-clear></a-input>
                </a-form-item>
                <!-- <a-form-item label="统一还款费率">
                    <a-radio-group :options="options1" v-model="config.rateUnion" />
                </a-form-item>
                <a-form-item label="快捷白名单开关">
                    <a-radio-group :options="options1" v-model="config.quickUserOn" />
                </a-form-item> -->
                <a-form-item label="隐藏开关">
                    <a-radio-group :options="options1" v-model="config.hideOnOff" />
                </a-form-item>
                <a-form-item  v-bind="formItemLayoutWithOutLabel">
                    <a-button type="primary" @click="updateConfig">立即修改</a-button>
                </a-form-item>
            </a-form>
        </div>
    </div>
</template>

<script>
import { queryBrandConfig , updateBrandConfig , updateBrandConfigKey } from '@/api/brand';
import { queryBrandUploadFiles , uploadBrandFile } from '@/api/power';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandList: [],
            config: {
                androidVersion: '', // 安卓版本号
                androidVersionCode: '', // 安卓版本号
                androidPackageName: '', // 安卓版本名称
                androidIsForceUpdate: '1', // 安卓是否强制更新
                androidContent: '', // 安卓升级说明
                androidDownloadUrl: '', // 安卓下载地址
                iosVersion: '', // 苹果版本号
                iosContent: '', // 苹果升级说明
                iosDownloadUrl: '', // 苹果下载地址
                shareUrl: '', // 分享地址
                externalDownloadUrl: '', // 安卓外部下载链接
                iosExternalDownloadUrl: '', // IOS外部下载地址
                iosVersionName:"",
                maxRealNameAuthCount: '',
                hideOnOff: '1',
                rateUnion: '0', // 统一还款费率开关
                quickUserOn: '0', // 快捷白名单开关
                deductRate: '', // 奖励扣除比例
                registerTaskAmount: '', // 引流任务奖励总金额
            },
            formItemLayoutWithOutLabel: {
                wrapperCol: {
                    xs: { span: 24, offset: 0 },
                    sm: { span: 20, offset: 6 },
                },
            },
            options : [
                { label: '强制更新', value: '1' },
                { label: '不强制', value: '0' },
            ],
            options1 : [
                { label: '开启', value: '1' },
                { label: '关闭', value: '0' },
            ],
            fileList: [],
            showAPKUpload: false,
            showIPAUpload: false,
            APKfile: {},
            IPAfile: {},
            confirmLoading: false,
            androidOssId: undefined,
            iosOssId: undefined,
            androidFileList: [],
            iosFileList: [],
        };
    },
    created(){
        this.getConfig()
    },
    methods: {
        getConfig(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'configKey':'', // 配置字段名称
                'isGroup':1, // 是否分组,0:否,1:是
            }
            queryBrandConfig(params)
            .then(res=>{
                if(res.success){
                    if(res.data.length > 0){
                        _this.config = res.data[0]
                    }
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 修改品牌信息
        async updateConfig(){
            let _this = this
            if(!_this.brandId){
                _this.$message.warning('请先选择品牌！')
                return
            }
            const params = {
                // 'brandId':_this.brandId,
                // 'androidVersion':_this.config.androidVersion,
                'androidVersionCode':_this.config.androidVersionCode,
                'androidVersionName':_this.config.androidVersionName,
                'androidPackageName':_this.config.androidPackageName,
                'androidIsForceUpdate':_this.config.androidIsForceUpdate,
                'androidContent':_this.config.androidContent,
                'androidDownloadUrl':_this.config.androidDownloadUrl,
                'iosVersion':_this.config.iosVersion,
                // 'iosContent':_this.config.iosContent,
                'iosDownloadUrl':_this.config.iosDownloadUrl,
                'shareUrl':_this.config.shareUrl,
                'externalDownloadUrl':_this.config.externalDownloadUrl,
                'iosExternalDownloadUrl':_this.config.iosExternalDownloadUrl,
                'iosVersionName':_this.config.iosVersionName,
                'hideOnOff':_this.config.hideOnOff,
                'maxRealNameAuthCount': _this.config.maxRealNameAuthCount,
                'rateUnion': _this.config.rateUnion,
                'wechat': _this.config.wechat,
                'phone': _this.config.phone,
                'quickUserOn': _this.config.quickUserOn,
                'deductRate': _this.config.deductRate,
                'registerTaskAmount': _this.config.registerTaskAmount,
            }
            for (const key in params) {
                // console.log(key,params[key])
                var pam = {
                    'brandId': _this.brandId,
                    'configKey': key,
                    'configValue': params[key],
                }
                await updateBrandConfigKey(pam)
                if(key == 'registerTaskAmount'){
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.getConfig()
                }
            }
            // updateBrandConfig(params)
            // .then(res=>{
            //     if(res.success){
            //         _this.$notification['success']({
            //             message: '提示',
            //             description: '修改成功',
            //             duration: 8,
            //         })
            //         this.getConfig()
            //     }else{
            //         _this.$notification['error']({
            //             message: '提示',
            //             description: res.errMessage,
            //             duration: 8,
            //         })
            //     }
            // })
        },
        beforeUpload(file) {
            // this.fileList = [...this.fileList, file];
            return false;
        },
        handleChangeAndroid(info) {
            const status = info.file.status;
            console.log(info)
            if(info.file.status === 'removed'){
                this.showAPKUpload = false
                this.APKfile = new Object()
                return
            }
            if(!this.brandId){
                this.$message.warning('请先选择品牌！')
                return
            }
            this.showAPKUpload = true
            this.APKfile = info.file
            this.androidFileList = info.fileList
            const AppInfoParser = require('app-info-parser')
            const parser = new AppInfoParser(info.file)
            parser.parse().then(result => {
                console.log('app info ----> ', result)
                // this.config.androidVersionCode = result.versionCode
                // this.config.androidVersionName = result.versionName
                // this.config.androidPackageName = result.package
                this.$set(this.config,'androidVersionCode',result.versionCode)
                this.$set(this.config,'androidVersionName',result.versionName)
                this.$set(this.config,'androidPackageName',result.package)
                // console.log(this.config,'config')
            // console.log('icon base64 ----> ', result.icon)
            }).catch(err => {
            console.log('err ----> ', err)
            })
            // if (status !== 'uploading') {
            //     console.log(info.file, info.fileList);
            // }
            // if (status === 'done') {
            //     this.$message.success(`${info.file.name} file uploaded successfully.`);
            // } else if (status === 'error') {
            //     // this.$message.error(`${info.file.name} file upload failed.`);
            // }
        },
        handleChangeIos(info){
            console.log(info)
            if(info.file.status === 'removed'){
                this.showIPAUpload = false
                this.IPAfile = new Object()
                return
            }
            if(!this.brandId){
                this.$message.warning('请先选择品牌！')
                this.fileList = []
                return
            }
            this.showIPAUpload = true
            this.IPAfile = info.file
            this.iosFileList = info.fileList
            const AppInfoParser = require('app-info-parser')
            const parser = new AppInfoParser(info.file)
            parser.parse().then(result => {
                console.log('app info ----> ', result)
                // this.config.iosVersion = result.CFBundleVersion
                this.$set(this.config,'iosVersion',result.CFBundleVersion)
            // console.log('icon base64 ----> ', result.icon)
            }).catch(err => {
            console.log('err ----> ', err)
            })
        },
        // 上传APK安卓包
        updateAPK(){
            let _this = this
            let formData = new FormData()
            formData.append('brandId',_this.brandId)
            formData.append('remark','安卓下载包')
            formData.append('file',_this.APKfile)
            _this.androidOssId && formData.append('id',_this.androidOssId)
            _this.confirmLoading = true
            _this.uploadOss(formData,1)
        },
        // 上传IPA苹果包
        updateIPA(){
            let _this = this
            let formData = new FormData()
            formData.append('brandId',_this.brandId)
            formData.append('remark','IOS下载包')
            formData.append('file',_this.IPAfile)
            _this.iosOssId && formData.append('id',_this.iosOssId)
            _this.confirmLoading = true
            _this.uploadOss(formData,2)
        },
        // 上传文件
        uploadOss(formData,type){
            let _this = this
            uploadBrandFile(formData)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '上传成功',
                        duration: 8,
                    })
                    if(type == 1){
                        _this.androidOssId = res.data.id
                        _this.$set(this.config,'androidDownloadUrl',res.data.fileUrl)
                    }else{
                        _this.iosOssId = res.data.id
                        _this.$set(this.config,'iosExternalDownloadUrl',res.data.fileUrl)
                    }
                    _this.confirmLoading = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 查询安卓下载包和IOS下载包地址
        getOssAddress(type){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'remark':type == 1 ? '安卓下载包' : 'IOS下载包',
                'page':0,
                'size':10,
            }
            queryBrandUploadFiles(params)
            .then(res=>{
                if(res.success){
                    if(res.data.content.length > 0){
                        if(type == 1){
                            _this.androidOssId = res.data.content[0].id
                        }else{
                            _this.iosOssId = res.data.content[0].id
                        }
                    }else{
                        if(type == 1){
                            _this.androidOssId = undefined
                        }else{
                            _this.iosOssId = undefined
                        }
                    }
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        }
    }
}

</script>
<style scoped>

</style>